package com.plan.service.card.impl;

import com.plan.base.config.property.AppProperty;
import com.plan.base.constant.BaseCode;
import com.plan.base.entity.Result;
import com.plan.controller.card.request.BaseReq;
import com.plan.controller.card.response.CardListVO;
import com.plan.controller.user.response.UserVO;
import com.plan.dao.mapper.card.TeamMapper;
import com.plan.dao.service.card.CardDaoService;
import com.plan.entity.card.Card;
import com.plan.entity.card.PointPrizeDrawSetting;
import com.plan.entity.card.Team;
import com.plan.enums.DelFlagEnum;
import com.plan.service.card.CardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 积分卡，也是孩子
 *
 * @author zcl
 */
@Service
public class CardServiceImpl implements CardService {
    @Resource
    private CardDaoService cardDaoService;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private AppProperty appProperty;

    @Override
    public Result<Long> insert(Card card) {
        Team team = teamMapper.selectOneById(card.getTeamId());
        if (team == null) {
            return Result.fail(BaseCode.TEAM_NOT_EXIST);
        }
        if (team.getCardNum() > appProperty.getTeamMaxCardNum()) {
            return Result.fail(BaseCode.TEAM_CARD_NUM_LIMIT);
        }
        cardDaoService.insert(card, team);
        return Result.ok(card.getId());
    }

    @Override
    public Result<Long> update(Card card) {
        Card old = cardDaoService.selectOneById(card.getTeamId(), card.getId());
        if (old == null) {
            return Result.fail(BaseCode.DATA_NOT_EXIST);
        }

        Card update = new Card();
        update.setId(card.getId());
        update.setTeamId(card.getTeamId());
        update.setName(card.getName());
        update.setGender(card.getGender());
        update.setBirthday(card.getBirthday());
        update.setVersion(old.getVersion());
        update.setUpdaterId(card.getUpdaterId());
        int num = cardDaoService.update(update);
        if (num == 0) {
            return Result.fail(BaseCode.DATA_BE_MODIFIED);
        }
        return Result.ok(card.getId());
    }

    @Override
    public Result<Long> delete(BaseReq req, UserVO vo) {
        Card old = cardDaoService.selectOneById(req.getTeamId(), req.getCardId());
        if (old == null) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        if (old.getCreatorId().compareTo(vo.getId()) != 0) {
            return Result.fail(BaseCode.NOT_THE_CREATOR);
        }
        Card card = new Card();
        card.setId(req.getCardId());
        card.setTeamId(req.getTeamId());
        card.setUpdaterId(vo.getId());
        card.setVersion(old.getVersion());
        card.setDelFlag(DelFlagEnum.DELETED.getType());

        Team team = teamMapper.selectOneById(req.getTeamId());
        Team update = new Team();
        update.setId(req.getTeamId());
        update.setCardNum(team.getCardNum() - 1);
        update.setVersion(team.getVersion());
        update.setUpdaterId(vo.getId());
        cardDaoService.delete(req, update, card);
        return Result.ok();
    }

    @Override
    public Result<List<CardListVO>> list(Long teamId) {
        List<CardListVO> list = cardDaoService.list(teamId);
        return Result.ok(list);
    }

}
